<?php

class Controller extends View {

	public $autoRender = true;
	
	public $HTTP = Object;

	public $useDB = false;

	public $filters;

	public $doNotInit = false;

	public $langs = array(
		"sr" => "Srpski"
	);

	public function __construct() {
		$this->init();
		parent::__construct();
	}

	public function init() {
		$this->HTTP = (Object)array_merge($_GET,$_POST);
		// load dbModel
		if($this->useDB !== false){
			$class = is_bool($this->useDB) ? get_class($this) : $this->useDB;
			$class = is_array($class) ? $class : array($class);
			foreach($class as $c){
				$this->dbLoad($c);
			}
		}

		// load language specific strings
		if(isset($this->request->named["lang"])) {
			$_SESSION["language"] = strtolower($this->request->named["lang"]);
			$url = preg_replace('/\/lang\;[a-z]{2}/', '', $_SERVER["REQUEST_URI"]);
			$url = empty($url) ? "/" : $url;
			redirect($url);
		}elseif(empty($_SESSION["language"])){
			$_SESSION["language"] = DEFAULT_LOCALE;
		}
	}

	public function dbLoad($class){
		$modelName = "db".$class;
		if(!$this->$modelName){
			$modelClassName = "Db".$class;
			$this->$modelName = new $modelClassName();
		}
	}

	public function flashMessage(){
		$ret = "";
		if(!empty($_SESSION["flash-message"])){
			$this->set("flash_message", $_SESSION["flash-message"]);
			$this->set("flash_class", $_SESSION["flash-class"]);
			$ret = $this->fetch("flash");

			unset($_SESSION["flash-message"]);
			unset($_SESSION["flash-class"]);
		}
		return $ret;
	}

	public function flashSet($message, $class=""){
		$_SESSION["flash-message"] = $message;
		$_SESSION["flash-class"] = $class;
	}

	public function is_active($link){
		$req = Dispatcher::_loadRequest($link);
		if(!$this->request){
			$this->request = (Object)Dispatcher::_loadRequest();
		}
		return $req["controller"] == $this->request->controller && $req["action"] == $this->request->action && array_diff($req["params"],$this->request->params)===array();
	}

	public function breadcrumbs_add($name, $link=""){
		$_SESSION["breadcrumbs"][] = array("name"=>$name, "link"=>$link);
	}
	public function breadcrumbs($name, $link=""){
		return $_SESSION["breadcrumbs"];
	}
	public function loadMenu($name="menu"){
		$tmp = parse_ini_file("lib/$name.ini");
		$menu = array();
		foreach($tmp as $k=>$v){
			if(strpos($k, ".")){
				list($parent, $child) = explode(".", $k, 2);
				if(!is_array($menu[$parent])){
					$menu[$parent] = array(
						"label" => $menu[$parent],
						"children" => array(
							$child => $v
						)
					);
				}else{
					$menu[$parent]["children"][$child] = $v;
				}
			}else{
				$menu[$k] = $v;
			}
		}
		return $menu;
	}
	public function saveMenu($data, $name){
		$filename = "lib/$name.ini";
		$out = "";
		foreach($data as $link=>$label){
			if(is_array($label)){
				$out .= "$link = ".$label["label"]."\r\n";
				foreach($label["children"] as $sublink=>$sublabel){
					$out .= "$link.$sublink=$sublabel\r\n";
				}
			}else{
				$out .= "$link = $label\r\n";
			}
		}
		return file_put_contents($filename, $out);
	}
}

?>